package entity

import (
	"errors"

	"shqsoft.cn/pasnj/base"

	"github.com/jinzhu/gorm"
)

// COMP_PHKH 托管中收导入
type COMP_PHKH struct {
	ID     int    `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	CUID   int    `gorm:"column:CUID"`
	ZZJGDM string `gorm:"column:ZZJGDM"`
	XDJJH  string `gorm:"column:XDJJH"`
	CUNM   string `gorm:"column:CUNM"`
}

// TableName 经营单位中收导入表
func (COMP_PHKH) TableName() string {
	return "T_COMP_PHKH"
}

// BeforeSave 保存前的触发器
func (i *COMP_PHKH) BeforeSave(tx *gorm.DB) (err error) {
	sqlEx := "select CUID from T_COMP_ACCO where DKJJH=?"
	row := tx.Raw(sqlEx, i.XDJJH).Row()
	row.Scan(&i.CUID)

	if i.CUID == 0 {
		sqlEx := "select ID from T_COMP_CLIE where ENCD=?"
		row := tx.Raw(sqlEx, i.ZZJGDM).Row()
		row.Scan(&i.CUID)
	}

	if i.CUID == 0 {
		sqlEx := "select ID from T_COMP_CLIE where CUNM=?"
		row := tx.Raw(sqlEx, i.CUNM).Row()
		row.Scan(&i.CUID)
	}

	if i.CUID == 0 {
		return errors.New("根据信贷借据号、组织机构代码以及客户名称均无法找到对应的客户信息，请修改！")
	}

	return nil
}

// COMP_PHKHEF 返回结构体
func COMP_PHKHEF() base.EntityFunc {
	return func() interface{} {
		entity := COMP_PHKH{}
		return &entity
	}
}

// COMP_PHKHESF 返回结构体数组
func COMP_PHKHESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []COMP_PHKH
		return &entities
	}
}

func init() {
	base.DaoPoolObj.Put("COMP_PHKH", base.NewDefaultDao(COMP_PHKHEF(), COMP_PHKHESF(), nil))
}
